package weaver.interfaces.its.action;

import cn.hutool.core.date.DateUtil;
import com.weaver.general.Util;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/11/19
 * @Description 生成编号Action
 * @Author AdminZm
 */
public class CIPGenerateNumberAction implements Action {

    BaseBean baseBean = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestId = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog("CIPGenerateNumberAction start:" + requestId + "、" + billTableName);
        try {
            RecordSet rs = new RecordSet();
            String sql = "select cipbh,bmdm,sqcb from " + billTableName + " where requestId = ?";
            String jlbh = null;
            String jl = null;
            String bmdm = null;
            String sqcb = null;
            rs.executeQuery(sql, requestId);
            if (rs.next()) {
                jlbh = rs.getString("cipbh");
                bmdm = rs.getString("bmdm");
                sqcb = rs.getString("sqcb");
            }
            if (StringUtils.isNotEmpty(jlbh)) {
                baseBean.writeLog("cipbh is not null.");
                return Action.SUCCESS;
            }

            String khdm = "";
            if (StringUtils.isNotEmpty(bmdm)) {
                rs.execute("SELECT dm FROM uf_cipbmdm WHERE id = " + bmdm);
                if (rs.next()) {
                    khdm = rs.getString("dm");
                }
            }
            // 获取当前年月
            String dqny = DateUtil.format(new Date(), "yyyy-MM");

            // 前缀
            String prefix = "CIP";
            String sign = "6";

            String index = getSelectNameMain("sqcb", String.valueOf(formid), sqcb);

            rs.executeQuery("SELECT id,lsh FROM uf_bhlshjlb WHERE lx = ? AND ny = ?", sign, dqny);
            int lshTemp = 0;
            int dataId = 0;
            if (rs.next()) {
                lshTemp = rs.getInt("lsh");
                dataId = rs.getInt("id");
            }
            if (dataId > 0) {
                int newLsh = lshTemp + 1;
                rs.executeUpdate("UPDATE uf_bhlshjlb SET lsh = ? WHERE id = ?", newLsh, dataId);
            } else {
                rs.executeUpdate("INSERT INTO uf_bhlshjlb (lx,lsh,ny) VALUES (?,?,?)", sign, 1, dqny);
            }
            jlbh = prefix + "-" + index + "-" + khdm + "-" + DateUtil.format(new Date(), "yyMM") + getNext(lshTemp, 3) ;
            rs.executeUpdate("UPDATE " + billTableName + " SET cipbh = ? WHERE requestId = ?", jlbh, requestId);
        } catch (Exception e) {
            baseBean.writeLog("CIPGenerateNumberAction error:" + e);
        }
        return Action.SUCCESS;
    }

    /**
     * 获取下一个编号，并转成指定位数
     * @param startValue  上一个编号
     * @param digit  位数
     */
    public static String getNext(Integer startValue, int digit) {
        AtomicInteger val = new AtomicInteger(startValue);
        int nextVal = val.incrementAndGet();
        String format = "%0" + digit + "d";
        return String.format(format, nextVal);
    }

    private String getSelectNameMain(String fieldName, String billid, String selectValue) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from workflow_billfield where fieldname = ? and billid = ? and viewtype=0", fieldName, billid);
        rs.next();
        int fieldId = Util.getIntValue(rs.getString("id"),-1);
        rs.executeQuery("select selectname from workflow_SelectItem where fieldid = ? and selectvalue = ?", fieldId, selectValue);
        rs.next();
        return Util.null2String(rs.getString("selectname"));
    }
}
